package dao.impl;

import dao.IProductDao;
import dao.IShopCartDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import pojo.Product;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShopCartDaoImpl implements IShopCartDao {
    @Override
    public void addProduct(Product product, int shopCartId, int number) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="insert into cartentry (productid,shopcartid,count) values(?,?,?)";
        try {
            queryRunner.update(FirstApp.getConn(),sql,product.getProductid(),shopCartId,number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateProduct(Product product, int shopCartId, int number) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update cartentry set count=? where shopcartid=? and productid=?";
        try {
            if(number==0)
            {
                deleteProduct(product,shopCartId);
            }
            else
            {
                queryRunner.update(FirstApp.getConn(),sql,number,shopCartId,product.getProductid());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteProduct(Product product, int shopCartId) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="delete from cartentry where productid=? and shopcartid=?";
        try {
            queryRunner.update(FirstApp.getConn(),sql,product.getProductid(),shopCartId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<Product> findAll() {
        List<Product> products=new ArrayList<Product>();
        IProductDao productDao=new ProductDaoImpl();
        QueryRunner queryRunner=new QueryRunner();
        String sql="select productid from cartentry";
        try {
            List<Object[]> list=queryRunner.query(FirstApp.getConn(),sql,new ArrayListHandler());
            int i=0;
            for (;;)
            {
                if(i<list.size())
                {
                    products.add(productDao.findById((Integer) list.get(i)[0]));

                    i++;
                }
                else{
                    break;
                }
            }
            return products;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> findByLikeName(String name) {
        List<Product> products=new ArrayList<Product>();
        List<Product> productList=new ArrayList<Product>();
        List<Product> productList1 = new ArrayList<Product>();
        IProductDao productDao=new ProductDaoImpl();
        QueryRunner queryRunner=new QueryRunner();
        String sql="select productid from cartentry";
        try {
            List<Object[]> list=queryRunner.query(FirstApp.getConn(),sql,new ArrayListHandler());
            int i=0;
            for (;;)
            {
                if(i<list.size())
                {
                    products.add(productDao.findById((Integer) list.get(i)[0]));

                    i++;
                }
                else{
                    break;
                }
            }
            productList=productDao.findByLikeName(name);
            for (int j=0;j<productList.size();j++)
            {
                for (int k=0;k<products.size();k++)
                {
                    if(productList.get(j).getProductName().equals(products.get(k).getProductName()))
                    {
                        productList1.add(productList.get(j));
                    }
                }
            }
            return productList1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
